﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace ORMDemo.Core.Validate
{
    public static class DataValidateExend
    {
        public static bool ValidateModel<T>(this T t)
        {
            Type type = typeof(T);
            foreach (var item in type.GetProperties())
            {
                if (item.IsDefined(typeof(BaseValidateAttribute), true))
                {
                    object value = item.GetValue(t);
                    var attributeList = item.GetCustomAttributes<BaseValidateAttribute>();
                    if (attributeList != null)
                    {
                        foreach (var attr in attributeList)
                        {
                            if (!attr.Validate(value))
                            {
                                return false;
                            }
                        }
                    }
                }
            }
            return true;
        }
    }
}
